package com.purong.purong.fragment;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.Log;

import com.dreamliner.easypermissions.AfterPermissionGranted;
import com.dreamliner.easypermissions.EasyPermissions;
import com.purong.purong.PermissionConstant;
import com.purong.purong.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by user on 2017/9/18.
 */
public abstract class BasePermFragment extends BaseFragment implements EasyPermissions.PermissionCallbacks{
    private boolean isNeverAskAgain = false;
//    protected String[] getPermissions() {
//        return new String[]{};
//    }
//
//    //运行时权限管理
//    protected void doPermissinosSuc() {
//
//    }
    protected abstract String[] getPermissions();

    //运行时权限管理
    protected abstract void doPermissinosSuc();
    @Override
    public void onPermissionsGranted(int i, List<String> list) {
        if (list.size() == getPermissions().length) {
            doPermissinosSuc();
        } else {
            Log.e("BaseAct", "onPermissionsGranted: 有部分权限没有允许");
        }
    }

    @Override
    public void onPermissionsDenied(int i, List<String> list) {
        isNeverAskAgain = EasyPermissions.checkDeniedPermissionsNeverAskAgain(this, PermissionConstant.getPermsName(list) + "没有允许，请打开“设置”允许所有权限。",
                R.string.setting, R.string.cancel, list);
    }
    public final static int RC_ALL_PERM = 0x100;
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @AfterPermissionGranted(RC_ALL_PERM)
    protected void checkPermission(boolean showDialog) {
        String[] perms = getPermissions();

        if (EasyPermissions.hasPermissions(getActivity(), perms)) {
            doPermissinosSuc();
        } else {
            if (!showDialog)
                return;
            String[] denieds = EasyPermissions.getDeniedPermissions(getActivity(), perms);
            if (!isNeverAskAgain) {
                EasyPermissions.requestPermissions(this, getActivity().getResources().getString(R.string.app_name)+"需要申请" + PermissionConstant.getPermsName(denieds) + "，请点击允许。", RC_ALL_PERM, denieds);
            } else {

                ArrayList<String> deniedList = new ArrayList<>();
                Collections.addAll(deniedList, denieds);
                isNeverAskAgain = EasyPermissions.checkDeniedPermissionsNeverAskAgain(this, PermissionConstant.getPermsName(denieds) + "没有允许，请打开“设置”允许所有权限。",
                        R.string.setting, android.R.string.cancel, deniedList);
            }
        }
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @AfterPermissionGranted(RC_ALL_PERM)
    protected void checkPermission() {

        String[] perms = getPermissions();

        if (EasyPermissions.hasPermissions(getActivity(), perms)) {
            doPermissinosSuc();
        } else {
            String[] denieds = EasyPermissions.getDeniedPermissions(getActivity(), perms);
            if (!isNeverAskAgain) {
                EasyPermissions.requestPermissions(this, "普融需要申请" + PermissionConstant.getPermsName(denieds) + "，请点击允许。", RC_ALL_PERM, denieds);
            } else {

                ArrayList<String> deniedList = new ArrayList<>();
                Collections.addAll(deniedList, denieds);
                isNeverAskAgain = EasyPermissions.checkDeniedPermissionsNeverAskAgain(this, PermissionConstant.getPermsName(denieds) + "没有允许，请打开“设置”允许所有权限。",
                        R.string.setting, android.R.string.cancel, deniedList);
            }
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
}
